package AlgorithmTest;

import com.sun.tools.javac.Main;

import java.util.Arrays;

/**
 * @author Yansoul
 * @date 2021/12/22 - 12:40
 * 人生到处知何似，应似飞鸿踏雪泥
 */
public class LeetCodeTest {
    public boolean carPooling(int[][] trips, int capacity) {
        if (trips[0][0] > capacity)
            return false;

        int distance = 0;
        for (int i = 0; i < trips.length; i++) {
            if (trips[i][2] > distance)
                distance = trips[i][2];
        }

        int[] diff = new int[distance];
        for (int i = 0; i < trips.length; i++) {
            diff[trips[i][1] - 1] += trips[i][0];
//            if (trips[i][2] != distance)
            diff[trips[i][2] - 1] -= trips[i][0];
//            System.out.println(Arrays.toString(diff));
        }

        int[] ans = new int[distance];
        ans[0] = diff[0];
        for (int i = 1; i < distance; i++) {
            ans[i] = ans[i - 1] + diff[i];
            if (ans[i] > capacity) {
//                System.out.println("第" + (i + 1) + "位置容量超出：" + ans[i]);
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) throws Exception {
        LeetCodeTest ac = new LeetCodeTest();
        int[][] trips = {{3,2,7},{3,7,9},{8,3,9}};
        System.out.println(ac.carPooling(trips, 11));
    }
}
